package com.example.lenovo.zxing_lxy;

import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.lenovo.zxing_lxy.common.BaseActivity;
import com.example.lenovo.zxing_lxy.utils.ToastUtil;
import com.example.lenovo.zxing_lxy.utils.permission.PermissionCallback;
import com.example.lenovo.zxing_lxy.utils.permission.Permissionhelp;
import com.example.lenovo.zxinglib.activity.CaptureActivity;
import com.joker.api.Permissions4M;

import static com.example.lenovo.zxing_lxy.utils.permission.Permissionhelp.REQ_CODE_PERMISSION;

/**
 * zxing二维码扫描ing
 * @Author 李巷阳
 * Created at 2017/10/25 16:10
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {


    private Button mBtOpenzxing;
    private TextView mTvResult;



    @Override
    protected void initView() {
        mBtOpenzxing = obtainView(R.id.btOpenzxing);
        mTvResult = obtainView(R.id.tvResult);

    }

    @Override
    protected void initData() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void setListener() {
        mBtOpenzxing.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.btOpenzxing:
                Permissionhelp.getPermission(mActivity, Manifest.permission.CAMERA, REQ_CODE_PERMISSION, new PermissionCallback() {
                    @Override
                    public void onSuccessful() {
                        startCaptureActivityForResult();
                    }
                });
                break;
        }
    }

    private void startCaptureActivityForResult() {
        Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
        startActivityForResult(intent, CaptureActivity.REQ_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
         String mStr = "";
        if(data!=null){
            mStr = data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT);
        }else{
            ToastUtil.show("二维码无效,扫码失败");
        }
        switch (requestCode) {
            case CaptureActivity.REQ_CODE:


                switch (resultCode) {
                    case RESULT_OK:
                        mTvResult.setText(mStr);  //or do sth
                        break;
                    case RESULT_CANCELED:
                        if (data != null) {
                            mTvResult.setText(mStr);
                        }
                        break;
                }
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
            grantResults) {
        Permissions4M.onRequestPermissionsResult(mActivity, requestCode, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
